<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.dvyId ? $t('crud.addBtn') : $t('crud.editBtn')"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      :label-width="$t('language') === '简体中文' ? '140px' : '230px'"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="$t('platform.logisticsCompany')"
        prop="dvyName"
      >
        <el-input
          v-model="dataForm.dvyName"
          maxlength="50"
          show-word-limit
          @blur="dataForm.dvyName = onHandleInputSpaces(dataForm.dvyName)"
        />
      </el-form-item>
      <el-form-item
        :label="$t('platform.companyHomepage')"
        prop="companyHomeUrl"
      >
        <el-input
          v-model="dataForm.companyHomeUrl"
          maxlength="255"
          @blur="dataForm.companyHomeUrl = onHandleInputSpaces(dataForm.companyHomeUrl)"
        />
      </el-form-item>
      <el-form-item
        :label="$t('platform.companyNumber') + '(' + $t('sysManagement.expressBird') + ')'"
        prop="dvyNo"
      >
        <el-input
          v-model="dataForm.dvyNo"
          maxlength="20"
          @blur="dataForm.dvyNo = onHandleInputSpaces(dataForm.dvyNo)"
        />
      </el-form-item>
      <el-form-item
        :label="$t('platform.companyNumber') + '(' + $t('sysManagement.quick100') + ')'"
        prop="dvyNoHd"
      >
        <el-input
          v-model="dataForm.dvyNoHd"
          maxlength="20"
          @blur="dataForm.dvyNoHd = onHandleInputSpaces(dataForm.dvyNoHd)"
        />
      </el-form-item>
      <el-form-item
        :label="$t('platform.companyNumber') + '(' + $t('sysManagement.quickAli') + ')'"
        prop="aliNo"
      >
        <el-input
          v-model="dataForm.aliNo"
          maxlength="20"
          @blur="dataForm.aliNo = onHandleInputSpaces(dataForm.aliNo)"
        />
      </el-form-item>
      <el-form-item
        :label="$t('platform.logisticsQueryInterface')"
        prop="queryUrl"
      >
        <el-input
          v-model="dataForm.queryUrl"
          maxlength="100"
          :disabled="dataForm.dvyId !== 0"
          @blur="dataForm.queryUrl = onHandleInputSpaces(dataForm.queryUrl)"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="visible = false"
        >
          {{ $t('remindPop.cancel') }}
        </div>
        <div
          class="primary-btn default-btn"
          @click="onSubmit()"
        >
          {{ $t('remindPop.confirm') }}
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshDataList'])
const dataRule = {
  dvyName: [{ required: true, message: $t('platform.errorTip4'), trigger: 'blur' }],
  dvyNo: [{ required: true, message: $t('platform.errorTip6'), trigger: 'blur' }],
  dvyNoHd: [{ required: true, message: $t('platform.errorTip6'), trigger: 'blur' }],
  aliNo: [{ required: true, message: $t('platform.errorTip6'), trigger: 'blur' }]
}

const dataFormRef = ref(null)
const visible = ref(false)
const dataForm = ref({
  dvyId: null,
  dvyName: null,
  companyHomeUrl: null,
  recTime: null,
  modifyTime: null,
  queryUrl: null,
  dvyNo: null,
  aliNo: null,
  dvyNoHd: null
})
const init = dvyId => {
  dataForm.value.dvyId = dvyId || 0
  visible.value = true
  nextTick(() => {
    dataFormRef.value.resetFields()
    if (dataForm.value.dvyId) {
      http({
        url: http.adornUrl('/admin/delivery/info/' + dataForm.value.dvyId),
        method: 'get',
        params: http.adornParams()
      }).then(({ data }) => {
        dataForm.value = data
      })
    }
  })
}
/**
 * 输入框纯空格处理
 */
const onHandleInputSpaces = value => {
  if (!value) {
    return
  }
  if (!value.trim()) {
    return value.trim()
  } else {
    return value
  }
}

// 表单提交
const onSubmit = () => {
  dataFormRef.value.validate(valid => {
    if (valid) {
      http({
        url: http.adornUrl('/admin/delivery'),
        method: dataForm.value.dvyId ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      }).then(() => {
        ElMessage({
          message: $t('remindPop.success'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            emit('refreshDataList')
          }
        })
      })
    }
  })
}
defineExpose({ init })
</script>
